package com.test.action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.exception.PasswordException;
import com.test.exception.UsernameException;
import com.test.service.LoginService;

public class LoginAction extends ActionSupport  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	
	private LoginService loginService;
	
//	private HttpServletRequest request;

//	private HttpServletResponse response;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		
		if( loginService.isLogin(username, password)){
		
			return SUCCESS;
		}
		else{
			
			return INPUT;
		}
		
		
		//username invalid
		
//		if( !"hello".equals(this.getUsername())){
//			
//			throw new UsernameException("username invalid");
//		}
//		else if(!"world".equals( this.getPassword() )){
//			
//			throw new PasswordException("password invalid!");
//		}
//		else{
//			
//			return SUCCESS;
//		}

//		if ("hello".equals(this.getUsername().trim())
//				&& "world".equals(this.getPassword().trim())) {
//			Map map = ActionContext.getContext().getSession();
//			map.put("user", "valid");
//			
////			ActionContext.getContext().put("zhangsan", "helloworld");
//			
////			request.setAttribute("zhangsan", "helloworld");
//			
//			HttpServletResponse response = ServletActionContext.getResponse();
//			
//			Cookie cookie = new Cookie("username",this.getUsername());
//			
//			cookie.setMaxAge(1000);
//			
//			response.addCookie(cookie);
//			
//			return "success";
//			
//		} else {
//			
//			this.addFieldError("username", "username or world is error! ");
//			return "failer";
//
//		}
	}

//	@SuppressWarnings("unchecked")
//	public String hello() throws Exception {
//		
//		System.out.println("login invoken");
//
//		if ("hello".equals(this.getUsername().trim())
//				&& "world".equals(this.getPassword().trim())) {
//			Map map = ActionContext.getContext().getSession();
//			map.put("user", "valid");
			
//			ActionContext.getContext().put("zhangsan", "helloworld");
			
//			request.setAttribute("zhangsan", "helloworld");
			
//			HttpServletResponse response = ServletActionContext.getResponse();
//			
//			Cookie cookie = new Cookie("username",this.getUsername());
//			
//			cookie.setMaxAge(1000);
//			
//			response.addCookie(cookie);
//			
//			return "success";
//			
//		} else {
//			
//			this.addFieldError("username", "username or world is error! ");
//			return "failer";
//
//		}
//	}
	
//	public void validate() {
//		// TODO Auto-generated method stub
//		if (null == this.getUsername() || "".equals(this.getUsername().trim())) {
//			this.addFieldError("username", "username should be hello");
//		}
//		if (null == this.getPassword() || "".equals(this.getPassword().trim())) {
//			this.addFieldError("password", "password should be world");
//		}
//	}

//	public void setServletRequest(HttpServletRequest arg0) {
//		
//		System.out.println("request");
//		this.request = arg0;
//	}
	
	
//	public void setServletResponse(HttpServletResponse arg0) {
//		// TODO Auto-generated method stub
//		this.response = arg0;
//	}
}
